package org.agilelang.db.toagile;

/*
 AgileLang - A fourth generation language(http://www.agilelang.org).

 Copyright (C) 2013 Roderick Cusirramos (roderick20@hotmail.com)

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JFrame;
import javax.swing.table.DefaultTableModel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.MutableTreeNode;
import org.agilelang.object.Controller;
import org.agilelang.object.Model;
import org.agilelang.object.attributes.Attribute;
import org.agilelang.object.attributes.Reference;

public class DlgDataBase extends javax.swing.JDialog {

    //Project project = new Project();
    List<Model> models = new ArrayList<Model>();
    List<Controller> controller_scaffold = new ArrayList<Controller>();
    List<Controller> controller_transacction = new ArrayList<Controller>();
    MutableTreeNode root = new DefaultMutableTreeNode("MenuBar");
    final DefaultTreeModel model = new DefaultTreeModel(root);
    DefaultTableModel tbm = new DefaultTableModel(
            new Object[][]{},
            new String[]{
                "Name Table", "Name Model(singularize)", "Description"
            });
    DefaultTableModel tbm_scaffold = new DefaultTableModel(
            new Object[][]{},
            new String[]{
                "Controller", "Model", "Description"
            });
    DefaultTableModel tbm_transaction = new DefaultTableModel(
            new Object[][]{},
            new String[]{
                "Controller", "Model Header", "Model Detail"
            });
    boolean status = false;

    public boolean isStatus() {
        return status;
    }

    public DlgDataBase(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton4 = new javax.swing.JButton();
        btnGeneratecode = new javax.swing.JButton();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel5 = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        txtHost = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        txtUser = new javax.swing.JTextField();
        txtPassword = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        txtDB = new javax.swing.JTextField();
        connect = new javax.swing.JButton();
        jPanel6 = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        txtName = new javax.swing.JTextField();
        txtVersion = new javax.swing.JTextField();
        txtAuthor = new javax.swing.JTextField();
        jScrollPane4 = new javax.swing.JScrollPane();
        txtDescription = new javax.swing.JTextArea();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblModels = new javax.swing.JTable();
        btnLoadModel = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblScaffold = new javax.swing.JTable();
        btnLoadScaffold = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        tblTransacction = new javax.swing.JTable();
        btnTransacction = new javax.swing.JButton();
        jPanel8 = new javax.swing.JPanel();
        btnAddMenu = new javax.swing.JButton();
        btnAddMenuItem = new javax.swing.JButton();
        jScrollPane5 = new javax.swing.JScrollPane();
        jTree1 = new javax.swing.JTree();
        jLabel6 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jButton4.setText("Cancel");

        btnGeneratecode.setText("Generate Code Agile");
        btnGeneratecode.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGeneratecodeActionPerformed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Data Base"));

        jLabel1.setText("Driver :");

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Mysql" }));

        jLabel2.setText("Host :");

        jLabel3.setText("User :");

        jLabel4.setText("Password :");

        jLabel5.setText("Data Base :");

        connect.setText("Connect");
        connect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                connectActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(txtHost, javax.swing.GroupLayout.PREFERRED_SIZE, 197, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtDB, javax.swing.GroupLayout.PREFERRED_SIZE, 197, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 196, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 6, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, 188, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtUser, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 187, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(connect, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel3)
                    .addComponent(txtUser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(3, 3, 3)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtHost, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel4)
                    .addComponent(txtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtDB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(connect))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel6.setBorder(javax.swing.BorderFactory.createTitledBorder("Application"));

        jLabel7.setText("Name : ");

        txtAuthor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtAuthorActionPerformed(evt);
            }
        });

        txtDescription.setColumns(20);
        txtDescription.setRows(5);
        jScrollPane4.setViewportView(txtDescription);

        jLabel8.setText("Version : ");

        jLabel9.setText("Author :");

        jLabel10.setText("Description : ");

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel7)
                    .addComponent(jLabel8)
                    .addComponent(jLabel9)
                    .addComponent(jLabel10))
                .addGap(22, 22, 22)
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel6Layout.createSequentialGroup()
                        .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtAuthor, javax.swing.GroupLayout.DEFAULT_SIZE, 203, Short.MAX_VALUE)
                            .addComponent(txtVersion)
                            .addComponent(txtName))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(jScrollPane4))
                .addContainerGap())
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtVersion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtAuthor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel10))
                .addGap(0, 9, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(339, Short.MAX_VALUE))
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(57, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Application", jPanel5);

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Models"));

        jScrollPane1.setViewportView(tblModels);

        btnLoadModel.setText("Load");
        btnLoadModel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLoadModelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 814, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnLoadModel))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(btnLoadModel)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 352, Short.MAX_VALUE))
                .addContainerGap())
        );

        jTabbedPane1.addTab("Models", jPanel2);

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Scaffold"));

        jScrollPane2.setViewportView(tblScaffold);

        btnLoadScaffold.setText("Load");
        btnLoadScaffold.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLoadScaffoldActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 814, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnLoadScaffold))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addComponent(btnLoadScaffold)
                .addContainerGap(340, Short.MAX_VALUE))
            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
        );

        jTabbedPane1.addTab("Controllers Scaffolds", jPanel3);

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Transacction"));

        jScrollPane3.setViewportView(tblTransacction);

        btnTransacction.setText("Load");
        btnTransacction.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTransacctionActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 814, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnTransacction))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                        .addComponent(btnTransacction)
                        .addGap(0, 329, Short.MAX_VALUE))
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                .addContainerGap())
        );

        jTabbedPane1.addTab("Controller Transactions", jPanel4);

        jPanel8.setBorder(javax.swing.BorderFactory.createTitledBorder("Menu"));

        btnAddMenu.setText("Add Menu");
        btnAddMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddMenuActionPerformed(evt);
            }
        });

        btnAddMenuItem.setText("Add MenuItem");
        btnAddMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddMenuItemActionPerformed(evt);
            }
        });

        jTree1.setModel(model);
        jScrollPane5.setViewportView(jTree1);

        javax.swing.GroupLayout jPanel8Layout = new javax.swing.GroupLayout(jPanel8);
        jPanel8.setLayout(jPanel8Layout);
        jPanel8Layout.setHorizontalGroup(
            jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel8Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane5)
                    .addGroup(jPanel8Layout.createSequentialGroup()
                        .addComponent(btnAddMenu)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnAddMenuItem)
                        .addGap(0, 671, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel8Layout.setVerticalGroup(
            jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel8Layout.createSequentialGroup()
                .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnAddMenu)
                    .addComponent(btnAddMenuItem))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane5, javax.swing.GroupLayout.DEFAULT_SIZE, 323, Short.MAX_VALUE)
                .addContainerGap())
        );

        jTabbedPane1.addTab("Menu", jPanel8);

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel6.setText("Database to Agile Lang");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTabbedPane1, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnGeneratecode)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton4)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(7, 7, 7)
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTabbedPane1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnGeneratecode)
                    .addComponent(jButton4))
                .addContainerGap())
        );

        jTabbedPane1.getAccessibleContext().setAccessibleName("Application");

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnLoadModelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLoadModelActionPerformed
        DlgSelectTables dlg = new DlgSelectTables(new JFrame(), true, models);
        dlg.setLocationRelativeTo(null);
        dlg.setVisible(true);
        if (dlg.isStatus()) {
            models = dlg.getModels();
            while (tbm.getRowCount() > 0) {
                tbm.removeRow(0);
            }
            int t = models.size();
            for (int x = 0; x < t; x++) {
                Model model = models.get(x);
                if (model.options.equals("true")) {
                    Object[] obj = {model.table, model.name, model.description};
                    tbm.addRow(obj);
                }
            }
        }
    }//GEN-LAST:event_btnLoadModelActionPerformed

    private void btnLoadScaffoldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLoadScaffoldActionPerformed
        DlgSelectScaffold dlg = new DlgSelectScaffold(new JFrame(), true, models, controller_scaffold);
        dlg.setLocationRelativeTo(null);
        dlg.setVisible(true);
        if (dlg.isStatus()) {
            controller_scaffold = dlg.getControllers();
            while (tbm_scaffold.getRowCount() > 0) {
                tbm_scaffold.removeRow(0);
            }
            int t = controller_scaffold.size();
            for (int x = 0; x < t; x++) {
                Controller controller = controller_scaffold.get(x);
                Object[] obj = {controller.name, controller.model_scaffold, controller.description};
                tbm_scaffold.addRow(obj);
            }
        }
    }//GEN-LAST:event_btnLoadScaffoldActionPerformed

    private void btnTransacctionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTransacctionActionPerformed
        Transacctions dlg = new Transacctions(new JFrame(), true, models, controller_transacction);
        dlg.setLocationRelativeTo(null);
        dlg.setVisible(true);
        if (dlg.isStatus()) {
            controller_transacction = dlg.getControllers();
            while (tbm_transaction.getRowCount() > 0) {
                tbm_transaction.removeRow(0);
            }
            int t = controller_transacction.size();
            for (int x = 0; x < t; x++) {
                Controller controller = controller_transacction.get(x);
                Object[] obj = {controller.name, controller.model_header, controller.model_detail};
                tbm_transaction.addRow(obj);
            }
        }


    }//GEN-LAST:event_btnTransacctionActionPerformed
    String file = "";
    String file_def = "";

    public String getFile_def() {
        return file_def;
    }

    public String getFile_model() {
        return file_model;
    }

    public String getFile_controller() {
        return file_controller;
    }

    public String getFile_menu() {
        return file_menu;
    }
    String file_model = "";
    String file_controller = "";
    String file_menu = "";

    public String getFile() {
        return file;
    }
    private void btnGeneratecodeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGeneratecodeActionPerformed

        int t = models.size();
        for (int x = 0; x < t; x++) {
            Model model1 = models.get(x);
            if (model1.options.equals("true")) {
                file_model += "Model." + model1.name + ".define( table = \"" + model1.table + "\", description = \"" + model1.description + "\" );\n";
                List<Attribute> attrs = model1.attributes;
                int ta = attrs.size();
                for (int y = 0; y < ta; y++) {
                    Attribute attr = attrs.get(y);
                    file_model += "Model." + model1.name + ".addAttribute( name = \"" + attr.name + "\", caption = \"" + attr.caption + "\", type = \"" + attr.type + "\", description = \"" + attr.description + "\" );\n";

                }
                List<Reference> references = model1.references;
                ta = references.size();
                for (int y = 0; y < ta; y++) {
                    Reference reference = references.get(y);
                    file_model += "Model." + model1.name + ".addReference(\n"
                            + "\tname_key = \"" + reference.name_key + "\",\n"
                            + "\tforeign_key = \"" + reference.foreign_key + "\",\n"
                            + "\tclass_reference = \"" + reference.model_reference + "\",\n"
                            + "\tattribute_reference = \"" + reference.attribute_reference + "\",\n"
                            + "\tfield_reference = \"" + reference.field_reference + "\",\n"
                            + "\tdescription = \"" + reference.description + "\"\n"
                            + ");\n";
                }
            }
            file_model += "\n";
        }
        t = controller_scaffold.size();
        for (int x = 0; x < t; x++) {
            Controller controller = controller_scaffold.get(x);
            file_controller += "Controller." + controller.name + ".define( type = \"scaffold\", model_scaffold = \"" + controller.model_scaffold + "\", description = \"" + controller.description + "\" );\n";
            file_controller += "\n";
        }

        t = controller_transacction.size();
        for (int x = 0; x < t; x++) {
            Controller controller = controller_transacction.get(x);
            file_controller += "Controller." + controller.name + ".define( type = \"transaction\", description = \"" + controller.description + "\");\n";
            file_controller += "Controller." + controller.name + ".addHeader( model = \"" + controller.model_header + "\");\n";
            file_controller += "Controller." + controller.name + ".addDetail( model = \"" + controller.model_detail + "\" ); \n";
            file_controller += "Controller." + controller.name + ".addRelation( header = \"" + controller.relation_header + "\", detail = \"" + controller.relation_detail + "\"); \n";
            file_controller += "\n";
        }
        //----------------------------------------------------------------------
        //menu
        t = model.getChildCount(root);


        for (int x = 0; x < t; x++) {
            DefaultMutableTreeNode obj = (DefaultMutableTreeNode) model.getChild(root, x);
            String name_pattern = obj.toString();
            String name_menu = "";
            String caption_menu = "";
            String description_menu = "";
            Pattern pattern = Pattern.compile("(name\\s*=\\s*\")(.+?)(\")");
            Matcher matcher = pattern.matcher(name_pattern);
            while (matcher.find()) {
                name_menu = matcher.group(2).toString();
            }
            pattern = Pattern.compile("(caption\\s*=\\s*\")(.+?)(\")");
            matcher = pattern.matcher(name_pattern);
            while (matcher.find()) {
                caption_menu = matcher.group(2).toString();
            }
            pattern = Pattern.compile("(description\\s*=\\s*\")(.+?)(\")");
            matcher = pattern.matcher(name_pattern);
            while (matcher.find()) {
                description_menu = matcher.group(2).toString();
            }

            file_menu += "Menu." + name_menu + ".define( caption = \"" + caption_menu + "\", description = \"" + description_menu + "\");\n";
            int tc = model.getChildCount(obj);
            for (int y = 0; y < tc; y++) {
                DefaultMutableTreeNode objc = (DefaultMutableTreeNode) model.getChild(obj, y);
                file_menu += "Menu." + name_menu + ".add" + objc.toString() + ";\n";
            }
        }

        file_def =
                "Application.config.define(\n"
                + "\tname = \"" + this.txtName.getText() + "\",\n"
                + "\tversion = \"" + this.txtVersion.getText() + "\",\n"
                + "\tauthor = \"" + this.txtAuthor.getText() + "\",\n"
                + "\tdescriprion = \"" + this.txtDescription.getText() + "\"\n"
                + ");\n\n"
                + "Application.config.db(\n"
                + "\tsource = \"mysql\",\n"
                + "\thost = \"" + this.txtHost.getText() + "\",\n"
                + "\tlogin = \"" + this.txtUser.getText() + "\",\n"
                + "\tpassword = \"" + this.txtPassword.getText() + "\",\n"
                + "\tdatabase = \"" + this.txtDB.getText() + "\"\n"
                + ");\n";




        status = true;
        this.setVisible(false);
        //System.out.println(file);
    }//GEN-LAST:event_btnGeneratecodeActionPerformed

    private void btnAddMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddMenuActionPerformed
        System.out.println("insert menu");
        DlgAddMenu dlg = new DlgAddMenu(new JFrame(), true);
        dlg.setLocationRelativeTo(null);
        dlg.setVisible(true);
        if (dlg.isStatus()) {
            MutableTreeNode menu = new DefaultMutableTreeNode("Menu( name = \"" + dlg.getTxtName() + "\", caption = \"" + dlg.getTxtCaption() + "\", description =\"" + dlg.getTxtDescription() + "\" )");
            model.insertNodeInto(menu, root, 0);
        }
        for (int i = 0; i < jTree1.getRowCount(); i++) {
            jTree1.expandRow(i);
        }
    }//GEN-LAST:event_btnAddMenuActionPerformed

    private void btnAddMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddMenuItemActionPerformed
        int t = model.getChildCount(root);
        DlgAddMenuItem dlg = new DlgAddMenuItem(new JFrame(), true);

        for (int x = 0; x < t; x++) {
            DefaultMutableTreeNode obj = (DefaultMutableTreeNode) model.getChild(root, x);
            String name = obj.toString();
            Pattern pattern = Pattern.compile("(name\\s*=\\s*\")(.+?)(\")");
            Matcher matcher = pattern.matcher(name);
            while (matcher.find()) {
                dlg.getCbxMenu().addItem(matcher.group(2));
            }
        }
        //HashMap<String,String> action = new HashMap<String,String>();
        t = controller_scaffold.size();
        for (int x = 0; x < t; x++) {
            Controller controller = controller_scaffold.get(x);
            dlg.getCbxController().addItem(controller.name);
        }
        t = controller_transacction.size();
        for (int x = 0; x < t; x++) {
            Controller controller = controller_transacction.get(x);
            dlg.getCbxController().addItem(controller.name);
        }
        dlg.setLocationRelativeTo(null);
        dlg.setVisible(true);
        if (dlg.isStatus()) {
            t = model.getChildCount(root);
            String name_dlg = dlg.getCbxMenu().getSelectedItem().toString();
            for (int x = 0; x < t; x++) {
                String name_aux = "";
                DefaultMutableTreeNode obj = (DefaultMutableTreeNode) model.getChild(root, x);
                String name = obj.toString();
                Pattern pattern = Pattern.compile("(name\\s*=\\s*\")(.+?)(\")");
                Matcher matcher = pattern.matcher(name);
                while (matcher.find()) {
                    name_aux = matcher.group(2);
                }
                if (name_aux.equals(name_dlg)) {
                    MutableTreeNode node_new = new DefaultMutableTreeNode("MenuItem( name = \"" + dlg.getTxtName().getText() + "\", caption = \"" + dlg.getTxtCaption().getText() + "\", controller = \"" + dlg.getCbxController().getSelectedItem().toString() + "\", action = \"crud\")");
                    model.insertNodeInto(node_new, obj, 0);
                    System.out.println(obj.toString());
                }
            }
            for (int i = 0; i < jTree1.getRowCount(); i++) {
                jTree1.expandRow(i);
            }
        }

        /**/
    }//GEN-LAST:event_btnAddMenuItemActionPerformed

    private void connectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_connectActionPerformed
        ReadDBMysql db = new ReadDBMysql(this.txtHost.getText(), this.txtDB.getText(), this.txtUser.getText(), this.txtPassword.getText());
        models = db.getListTables();
        tblModels.setModel(tbm);
        tblScaffold.setModel(tbm_scaffold);
        tblTransacction.setModel(tbm_transaction);
        /*---------------------------------------*/
        DlgSelectTables dlg = new DlgSelectTables(new JFrame(), true, models);
        dlg.setLocationRelativeTo(null);
        dlg.setVisible(true);
        if (dlg.isStatus()) {
            models = dlg.getModels();
            while (tbm.getRowCount() > 0) {
                tbm.removeRow(0);
            }
            int t = models.size();
            for (int x = 0; x < t; x++) {
                Model model = models.get(x);
                if (model.options.equals("true")) {
                    Object[] obj = {model.table, model.name, model.description};
                    tbm.addRow(obj);
                }
            }
        }
    }//GEN-LAST:event_connectActionPerformed

    private void txtAuthorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtAuthorActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtAuthorActionPerformed
    /**
     * @param args the command line arguments
     *
     * public static void main(String args[]) {
     *
     *
     * java.awt.EventQueue.invokeLater(new Runnable() { public void run() {
     * DlgDataBase dialog = new DlgDataBase(new javax.swing.JFrame(), true);
     * dialog.addWindowListener(new java.awt.event.WindowAdapter() {
     * @Override public void windowClosing(java.awt.event.WindowEvent e) {
     * System.exit(0); } }); dialog.setVisible(true); } });
    }
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAddMenu;
    private javax.swing.JButton btnAddMenuItem;
    private javax.swing.JButton btnGeneratecode;
    private javax.swing.JButton btnLoadModel;
    private javax.swing.JButton btnLoadScaffold;
    private javax.swing.JButton btnTransacction;
    private javax.swing.JButton connect;
    private javax.swing.JButton jButton4;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTree jTree1;
    private javax.swing.JTable tblModels;
    private javax.swing.JTable tblScaffold;
    private javax.swing.JTable tblTransacction;
    private javax.swing.JTextField txtAuthor;
    private javax.swing.JTextField txtDB;
    private javax.swing.JTextArea txtDescription;
    private javax.swing.JTextField txtHost;
    private javax.swing.JTextField txtName;
    private javax.swing.JTextField txtPassword;
    private javax.swing.JTextField txtUser;
    private javax.swing.JTextField txtVersion;
    // End of variables declaration//GEN-END:variables
}
